#include "ui_plottemperature.h"
#include "plottemperature.h"
#include "qcustomplot.h"

#define QWHITE QColor(0xff, 0xff, 0xff)
#define QBLACK QColor(0x2c, 0x2c, 0x2c)
#define QDEEPBLUE QColor(0x00, 0x00, 0xff)
#define QORANGE QColor(0xff, 0x77, 0x00)
#define QPURPLE QColor(0xd4, 0x23, 0x7a)
#define QBLUE QColor(0x12, 0x96, 0xdb)
#define QGREEN QColor(0x1a, 0xfa, 0x29)
#define QYELLOW QColor(0xf4, 0xea, 0x2a)
#define QRED QColor(0xd8, 0x1e, 0x06)

PlotTemperature::PlotTemperature(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PlotTemperature)
{
    ui->setupUi(this);
    init();
}

PlotTemperature::~PlotTemperature()
{
    delete ui;
}

void PlotTemperature::init()
{
    //    for (int i = 0; i < 8; ++i) {
    //        ui->mainPlot->addGraph();
    //        ui->mainPlot->graph(i)->setPen(QPen(SpectrumShowColorList[i]));
    //    }

    ui->plotTemp->legend->setVisible(true);
    ui->plotTemp->legend->setFont(QFont("Arial", 11));
    ui->plotTemp->legend->setRowSpacing(-3);
    ui->plotTemp->legend->setMargins(QMargins(200,5,10,5));
    ui->plotTemp->legend->setFillOrder(QCPLayoutGrid::foColumnsFirst);
    ui->plotTemp->legend->setWrap(6);
    ui->plotTemp->plotLayout()->addElement(1 , 0, ui->plotTemp->legend);
    ui->plotTemp->plotLayout()->setRowStretchFactor(1, 0.001);
    ui->plotTemp->legend->setBorderPen(Qt::NoPen);
    //ui->plotTemp->axisRect()->insetLayout()->setInsetAlignment(0,Qt::AlignTop|Qt::AlignHCenter);


    ui->plotTemp->addGraph();
    ui->plotTemp->graph(0)->setPen(QPen(QBLUE));
    ui->plotTemp->graph(0)->setName(tr("Substrate"));
    ui->plotTemp->graph()->setLineStyle((QCPGraph::LineStyle)QCPGraph::lsLine);
    ui->plotTemp->graph()->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 3));//设置散点形状
    ui->plotTemp->addGraph();
    ui->plotTemp->graph(1)->setPen(QPen(QRED));
    ui->plotTemp->graph(1)->setName(tr("Heating module"));
    ui->plotTemp->graph()->setLineStyle((QCPGraph::LineStyle)QCPGraph::lsLine);
    ui->plotTemp->graph()->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDiamond, 3));//设置散点形状

    ui->plotTemp->xAxis->setRange(0,600);
    ui->plotTemp->xAxis->ticker()->setTickCount(10);
    ui->plotTemp->yAxis->setRange(0,300);
    ui->plotTemp->yAxis->ticker()->setTickCount(15);

    ui->plotTemp->xAxis->setBasePen(QPen(QBLACK));
    ui->plotTemp->yAxis->setBasePen(QPen(QBLACK));

    ui->plotTemp->xAxis->setTickPen(QPen(QBLACK));
    ui->plotTemp->yAxis->setTickPen(QPen(QBLACK));

    ui->plotTemp->xAxis->setSubTickPen(QPen(QBLACK));
    ui->plotTemp->yAxis->setSubTickPen(QPen(QBLACK));

    ui->plotTemp->xAxis->setTickLabelColor(QBLACK);
    ui->plotTemp->yAxis->setTickLabelColor(QBLACK);

    ui->plotTemp->xAxis->setLabel(tr("time/s"));
    ui->plotTemp->xAxis->setLabelFont(QFont("Arial", 11)); // 设置字体和大小
    ui->plotTemp->xAxis->setLabelColor(QDEEPBLUE); // 设置标签颜色
    ui->plotTemp->xAxis->setLabelPadding(10); // 设置标签与轴之间的间距

    ui->plotTemp->yAxis->setLabel(tr("temperature/°c"));
    ui->plotTemp->yAxis->setLabelFont(QFont("Arial", 11)); // 设置字体和大小
    ui->plotTemp->yAxis->setLabelColor(QDEEPBLUE); // 设置标签颜色
    ui->plotTemp->yAxis->setLabelPadding(10); // 设置标签与轴之间的间距

    ui->plotTemp->setBackground(QBrush(QWHITE));//设置背景色
    ui->plotTemp->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
    ui->plotTemp->replot();
}


void PlotTemperature::drawPlot(int index ,double time,double data)
{
    ui->plotTemp->graph(index)->addData(time,data);
    ui->plotTemp->rescaleAxes(true);
    ui->plotTemp->replot();
}

void PlotTemperature::clearPlot()
{
    ui->plotTemp->graph(0)->data()->clear();
    ui->plotTemp->graph(1)->data()->clear();
    ui->plotTemp->rescaleAxes(true);
    ui->plotTemp->replot();
}

